<?php
class TourismCart extends BZ_Controller
{
    public function _init()
    {
        $this->load->model('tourism_goods_attr_model', 'tourism_goods_attr');
        $this->load->model('tourism_cart_goods_model', 'tourism_cart_goods');
        $this->load->model('tourism_enshrine_model', 'tourism_enshrine');
        $this->load->model('tourism_goods_category_spec_model','tourism_goods_category_spec');
        $this->load->model('tourism_provider_model','tourism_provider');
    }

    /**
     * 购物车
     */
    public function index()
    {
        if (!$this->uid) {
            $this->load->view('tourism/cart/nologin', $this->data);
        } else {
            $cartGoods = $this->tourism_cart_goods->findTourismCart($this->uid);

            if ($cartGoods->num_rows() <= 0) {
                $this->load->view('tourism/cart/empty', $this->data);
            } else {
                $cart = array();
                $cartDisabled = array();
                foreach ($cartGoods->result() as $key => $item) {
                    //产品下架  或购买数量大于库存数量
                    if($item->is_on_sale == 2 || $item->number < $item->goods_num || $item->is_check != 2){
                        $cartDisabled[$key] = $item;
                    } else {
                        $cart[$item->uid]['alias_name'] = $item->alias_name;
                        $cart[$item->uid]['uid'] = $item->uid;
                        $cart[$item->uid]['shop_goods'][] = $item;
                    }
                }
                $this->data['cart'] = $cart;
                $this->data['cartDisabled'] = $cartDisabled;
                $this->load->view('tourism/cart/cart', $this->data);
            }
        }
    }
    
    /**
     * 加入购物车
     */
    public function addCart()
    {
        $specArray = '';
        $spec_id = $this->input->post('sp');
        $goodsAttrId = $this->input->post('id');
        $number = $this->input->post('number') ? $this->input->post('number') : 1;
        //判断是否存在该商品 切商品仍然有库存 //加入商品价格记录 判断商品是否重复

        $result = $this->tourism_goods_attr->findByCartGoods($goodsAttrId, $this->uid);
        if ($result->num_rows() <= 0) {
            $this->jsonMessage('该产品不存在');
        }
        $result = $result->row();
        if ($result->number <= ($number + $result->goods_num)) {
            $this->jsonMessage('库存紧张，最多只能买'.$number.'件哦！');
        }
        if (!empty($spec_id)) {
            $specValue = $this->tourism_goods_category_spec->findBySpec($spec_id);
            if ($specValue->num_rows()>0) {
                foreach ($specValue->result() as $item) {
                    $spec[] = $item->attribute_value;
                }
                $specArray = join(',',$spec); //选择规格属性
            }
        }
        if (!empty($result->id)) {
            $status = $this->tourism_cart_goods->updateGoodsNum($result->id, $number,$specArray);
        } else {
            $status = $this->tourism_cart_goods->insertCart($this->uid,$goodsAttrId,$specArray,$number);
        }
        if ($status) {
            $this->jsonMessage('');
        } else {
            $this->jsonMessage('很遗憾，商品移至购物车失败');
        }
    }
    
    /*
     *数量变更判断
     * */
    public function updateCart()
    {
        $goodsAttrId = $this->input->post('id');
        $num = $this->input->post('num');
        $result = $this->tourism_cart_goods->findByGoodsAttrId($goodsAttrId, $this->uid);
        if ($result->num_rows() > 0) {
            $row = $result->row();
            $status = false;
            if (($row->limit_num < $num) && ($row->limit_num > 0)) {  //限购
                $num = $row->limit_num;
                echo json_encode(
                        array('status'=>false, 'messages'=>'最多只能买'.$num.'件哦！', 'goods_num'=>$num)
                );exit;
            }
            if ($row->goods_num < $num) {
                $num = $row->goods_num;
                $status = true;
            }
            $flag = $this->tourism_cart_goods->update($row->id, array('goods_num'=>$num));
            if ($flag) {
                if ($status) {
                    echo json_encode(
                        array('status'=>false, 'messages'=>'库存紧张，最多只能买'.$num.'件哦！', 'goods_num'=>$num)
                    );exit;
                }
                $this->jsonMessage('',$num);
            } else {
                $this->jsonMessage('操作失败！');
            }
        }
        $this->jsonMessage('该商品不存在！');
    }

    public function validateGoodsNum()
    {
        $goodsAttrId = $this->input->post('id');
        $num = $this->input->post('num');
        $result = $this->tourism_goods_attr->findGoodsBase($goodsAttrId, $this->uid);
        if ($result->num_rows() > 0) {
            $row = $result->row();
            $status = false;
            if ($row->goods_num <= $num) {
                $num = $row->goods_num;
                $status = true;
            }
            if ($status) {
                echo json_encode(
                    array('status'=>false, 'messages'=>'库存紧张，最多只能买'.$num.'件哦！', 'goods_num'=>$num)
                );exit;
            }
            $this->jsonMessage('',$num);
        }
        $this->jsonMessage('该商品不存在！');
    }

    /**
     *
     */
    public function changeSelected()
    {
        $uid = $this->input->post('uid');
        $ids = $this->input->post('id');

        $cartGoods = $this->tourism_cart_goods->findTourismCart($this->uid);
        $cart = array();
        $total = 0;
        $num = 0;
        $cartDisabled = array();
        foreach ($cartGoods->result() as $key => $item) {
            if ($item->is_on_sale == 2 || $item->number < $item->goods_num || $item->is_check != 2) {
                $cartDisabled[$key] = $item;
            } else {
                if (!empty($ids) && in_array($item->goods_attr_id, $ids)) {
                    $item->status = true;
                    $total += bcmul($item->shop_price, $item->goods_num, 2);
                    $num += $item->goods_num;
                }
                if (!empty($uid) && in_array($item->uid,$uid)) {
                    $cart[$item->uid]['status'] = true;
                }
                $cart[$item->uid]['alias_name'] = $item->alias_name;
                $cart[$item->uid]['uid'] = $item->uid;
                $cart[$item->uid]['shop_goods'][] = $item;
            }
        }
        $data['cart'] = $cart;
        $data['cartDisabled'] = $cartDisabled;
        echo json_encode(array(
            'status' => true,
            'total'  => $total,
            'num'    => $num,
            'html'   => $this->load->view('tourism/cart/main', $data, true)
        ));exit;
    }
    
    /*
    *购物车删除
    * */
    public function cartDelete()
    {
        $id = $this->input->post('id');
        if (empty($id)) {
            $this->jsonMessage('商品移除失败');
        }
        $result = $this->tourism_cart_goods->delete($id, $this->uid);
        if ($result) {
            $this->jsonMessage('');
        }
        $this->jsonMessage('商品移除失败');
    }

    public function ajaxShrine()
    {
        $goodsAttrId = $this->input->post('id');
        if (empty($goodsAttrId)) {
            $this->jsonMessage('商品编号不存在');
        }
        $isExist = $this->tourism_enshrine->findIsExist($goodsAttrId, $this->uid);
        if ($isExist->num_rows() > 0) {
            $row = $isExist->row();
            $result = $this->tourism_enshrine->delete(['uid' => $this->uid, 'id' => $row->id]);
            $flag = 'no';
        } else {
            $result = $this->tourism_enshrine->insertTourismEnshrine($goodsAttrId, $this->uid);
            $flag = 'yes';
        }
        if ($result) {
            $this->jsonMessage('',$flag);
        } else {
            $this->jsonMessage('收藏失败');
        }
    }

    public function checkLogin()
    {
        if ($this->frontUser) {
            $this->jsonMessage('');
        } else {
            echo json_encode(array(
                'status'   => false,
                'messages' => site_url('login/login/index')
            ));
        }
        exit;
    }

    public function clearCartDisabled()
    {
        $result = $this->tourism_cart_goods->findCartDisabled($this->uid);
        if ($result->num_rows() > 0) {
            $id = array();
            foreach ($result->result() as $value) {
                $id[] = $value->id;
            }
            $delete = $this->tourism_cart_goods->delete($id, $this->uid);
            if ($delete) {
                $this->jsonMessage('');
            }
        }
        $this->jsonMessage('您的购物车没有失效的产品，请刷新从新选择');
    }
}